package cn.tedu.contorller;

import cn.tedu.dao.CategoryDao;
import cn.tedu.dao.ProductDao;
import cn.tedu.entity.Category;
import cn.tedu.entity.Product;
import cn.tedu.entity.User;
import cn.tedu.utils.ThUtils;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/DetailServlet")
public class DetailServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        Context context = new Context();
        ProductDao pDao = new ProductDao();

        HttpSession session = request.getSession();

        //判断当前用户是否浏览过该作品
        String viewId = (String)session.getAttribute("view"+id);
        if (viewId==null){
            pDao.addviewCount(id);
            session.setAttribute("view"+id,id);
        }

        //通过ID获取作品
        Product p = pDao.findById(id);
        context.setVariable("p",p);

        //获取登录信息
        User user = (User)session.getAttribute("user");
        context.setVariable("user",user);

        //获取导航栏信息
        CategoryDao cdao = new CategoryDao();
        List<Category> list = cdao.findAll();
        context.setVariable("list",list);

        //获取浏览信息
        List<Product>viewList = pDao.findViewList();
        context.setVariable("viewList",viewList);

        //获取获赞信息
        List<Product>listList = pDao.findLikeList();
        context.setVariable("likeList",listList);

        ThUtils.print("detail.html",context,response);
    }
}
